#ifndef _TCP_SERVER_
#define _TCP_SERVER_

#ifndef _DEBUG
	#ifndef _NET_API_
		#define _NET_API_ _declspec(dllexport)
	#endif
#else
		#define  _NET_API_
#endif

#include "itcpserver.h"

class tcpserver : public ITCPServer
{
public:
	tcpserver();
	~tcpserver();
public:
	int StartServer( PORT listenPort, int maxCon = MIN_CONN );
	int StopServer( );
	int ActionServer();
#ifdef _LINUX
	int OpenConnect( int EpollFD, SOCKET sock );
	void AddConnToRecvEpollFD(int ConnIdx);
	void DelConnToRecvEpollFD(int ConnIdx);
#endif // _LINUX
	int OpenConnect( SOCKET sock );
	int CloseConnect( int connectIdx );

	int RemoveConnect(int connectIdx);

	void* Peek(int connectIdx,unsigned int datasize);
	void *PeekSendBuff(int connectIdx,unsigned int datasize);

	const void *GetDataFromRecvBuffByLength(int connectIdx,void* data, unsigned int datasize);
	const void* GetDataFromSendBuffByLength(int connectIdx,void *data, unsigned int datasize);

	int PushDataToSendBuff( int connectIdx,  void* pBuf, int dataSize );
	const void* GetDataFromRecvBuff(int connectIdx, unsigned int& dataSize );

	int PushDataToRecvBuff(int connectIdx, void* pBuf, int dataSize );
	const void* GetDataFromSendBuff(int connectIdx, unsigned int& dataSize );

	int CloseConnectCallBack(int connectIdx);
	int OpenConnectCallBack(int connectIdx);
public:

	int	getMaxConn()
	{
		return m_MaxConn;
	}
	TCPConnection* getConn( int connIdx )
	{
		if ( connIdx < 0 || connIdx >= m_MaxConn )
			return NULL;
		return &m_Conn[connIdx];
	}

	CONNECTINDEX* getConnectedIdxList()
	{
		return &m_ConnectedConnIdx;
	}
private:
	int	popFreeConnectIdx();
	int	pushFreeConnectIdx( int freeIdx );
private:
	int Init( );
	int Release( );
	int ReleaseConnectInfo( int connectIdx );
private:


	tcpListenThread m_ListenThread;
	tcpSendThread	m_SendThread;
	tcpRecvThread	m_RecvThread;


	TCPConnection*	m_Conn;
	int				m_MaxConn;
	CONNECTINDEX	m_FreeConnIdx;
	CONNECTINDEX	m_ConnectedConnIdx;
};


#endif